package com.quwan.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tms.v20201229.TmsClient;
import com.tencentcloudapi.ims.v20201229.ImsClient;
import com.tencentcloudapi.ims.v20201229.models.*;
import com.tencentcloudapi.tms.v20201229.models.TextModerationRequest;
import com.tencentcloudapi.tms.v20201229.models.TextModerationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ContentSecurityUtil {
    private static final Logger logger = LoggerFactory.getLogger(ContentSecurityUtil.class);

    /**
     * accessKeyId
     */
    private String secretId;
    /**
     * secretAccessKey
     */
    private String secretKey;
    /**
     * tmsEndpoint
     */
    private String tmsEndpoint;
    /**
     * imsEndpoint
     */
    private String imsEndpoint;

    private String region;

    public ContentSecurityUtil(String secretId, String secretKey, String tmsEndpoint,String imsEndpoint,String region) {
        this.secretId = secretId;
        this.secretKey = secretKey;
        this.tmsEndpoint = tmsEndpoint;
        this.imsEndpoint = imsEndpoint;
        this.region = region;
    }

    private TmsClient tmsClient;
    private ImsClient imsClient;

    /**
     * 创建TmsClient
     */
    public void createTmsClient() {
        Credential cred = new Credential(secretId, secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(tmsEndpoint);
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // clientProfile 可选
        this.tmsClient = new TmsClient(cred, region, clientProfile);
    }

    /**
     * 创建TmsClient
     */
    public void createImsClient() {
        Credential cred = new Credential(secretId, secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(imsEndpoint);
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // clientProfile 可选
        this.imsClient = new ImsClient(cred, region, clientProfile);
    }

    /**
     * 获取文本检测Response
     * @param content 文本内容
     * @return
     */
    public TextModerationResponse getTextModerationResponse (String content){
        TextModerationRequest req = new TextModerationRequest();
        req.setContent(Base64Convert.stringToBase64(content));
        try {
            TextModerationResponse resp = tmsClient.TextModeration(req);
            return resp;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取文本检测Response
     * @param content 文本内容
     * @return
     */
    public ImageModerationResponse getImageModerationResponse (String content){
        ImageModerationRequest req = new ImageModerationRequest();
        req.setFileContent(content);
//        req.setBizType("qudan_2021");
//        req.setFileUrl("https://resource.quqdan.com/image/5ed7593b-0d9e-4b76-addb-5115332dd2b620211202.png");
        try {
            ImageModerationResponse resp = imsClient.ImageModeration(req);
            return resp;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }
}
